import {onMounted, Ref, ref} from 'vue'
import axios from "@/utils/axios";
import User from "@/hooks/mix/user/user";
import {message} from "ant-design-vue";

export default ()=>{
    const user = ref(new User());
    const userList = ref(new Array());

    /**
     * 添加用户
     * @param name 用户名
     * @param tel 电话
     */
    const addUser = (name:String, tel:String)=>{
        axios
            .post("/mix/user/addUser",{
                name: user.value.name,
                tel: user.value.tel
            })
            .then((res:any) => {
                if(res.success) getUserList()
            });
    }

    /**
     * 删除用户
     * @param id 用户id
     */
    const delUser = (id:Number)=>{
        axios
            .post("/mix/user/delUser",{
                id: id
            })
            .then((res) => {
                userList.value.splice(userList.value.findIndex((item:any)=>item.id == id), 1);
                message.success("已删除");
            });
    }

    /**
     * 获取所有用户
     */
    const getUserList = ()=>{
        axios
            .post("/mix/user/getAllUser")
            .then((res) => {
                userList.value = res.data;
            });
    }

    return {
        user,
        userList,
        addUser,
        delUser,
        getUserList
    }
}